package com.liaowei.article.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liaowei.article.model.param.ArticleStatusParam;
import com.liaowei.article.model.param.QueryManageArticleParam;
import com.liaowei.article.model.param.SaveArticleParam;
import com.liaowei.article.service.ArticleService;
import com.liaowei.common.BaseResponse;
import com.liaowei.common.ResultUtils;
import com.liaowei.model.domain.Article;
import com.liaowei.model.param.QueryArticleParam;
import com.liaowei.model.vo.ArticleVO;
import com.liaowei.model.vo.UserVO;
import com.liaowei.model.vo.WordCloud;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;

@RestController
@RequiredArgsConstructor
@Validated
public class ArticleController {

    private final ArticleService articleService;



    // 查询搜索发现--随机取10条
    @GetMapping("discovery")
    public BaseResponse<List<Article>> discoveryArticle(String title){
        return ResultUtils.success(articleService.searchDiscoveryArticleList(title));
    }

    // 查询推荐文章
    @GetMapping("recommend")
    public BaseResponse<List<ArticleVO>> recommendArticle(){
        return ResultUtils.success(articleService.searchRecommendArticleVOList());
    }

    // 查询自己的文章
    @GetMapping("ownArticleVO")
    public BaseResponse<List<ArticleVO>> ownArticleVO(@RequestParam @NotNull(message = "用户id不能为空") Long userId,
                                                      String title) {
        return ResultUtils.success(articleService.searchOwnArticleVOList(userId,title));
    }

    // 查询文章热门排行
    @GetMapping("hotArticle")
    public BaseResponse<List<ArticleVO>> hotArticleList() {
        return ResultUtils.success(articleService.searchHotArticleList());
    }

    // 查询热点搜索词
    @GetMapping("wordClouds")
    public BaseResponse<Set<WordCloud>> wordCloudList(){
        return ResultUtils.success(articleService.searchWordCloudList());
    }

    // 根据userId查询文章数量
    @GetMapping("count/{userId}")
    public BaseResponse<Long> countOfArticleByUserId(@PathVariable
                                                     @Min(value = 1, message = "请输入合法的id")
                                                     Long userId) {
        return ResultUtils.success(articleService.searchCountOfArticleByUserId(userId));
    }

    // 删除文章
    @DeleteMapping("{articleId}/{userId}")
    public BaseResponse<String> removeArticle(@PathVariable
                                              @Min(value = 1, message = "请输入合法id")
                                              Long articleId,
                                              @PathVariable
                                              @Min(value = 1, message = "请输入合法id")
                                              Long userId) {
        return ResultUtils.success(articleService.removeArticleByIdAndUserId(articleId, userId));
    }


    // 根据id查询文章信息
    @GetMapping("{articleId}")
    public BaseResponse<Article> articleById(@PathVariable
                                             @Min(value = 1, message = "请输入合法的id")
                                             Long articleId) {
        return ResultUtils.success(articleService.searchArticleById(articleId));
    }

    // 查询文章详细信息以及和用户的状态
    @GetMapping("{articleId}/{userId}")
    public BaseResponse<ArticleVO> articleVo(@PathVariable
                                             @Min(value = 1, message = "请输入合法的id")
                                             Long articleId,
                                             @PathVariable
                                             @Min(value = 1, message = "请输入合法的id")
                                             Long userId) {
        return ResultUtils.success(articleService.searchArticleVo(articleId, userId));
    }

    // 通过id列表查询文章列表
    @PostMapping("pageByIds")
    public BaseResponse<List<Article>> pageByIds(@RequestBody List<Long> articleIds) {
        return ResultUtils.success(articleService.searchArticleListByIds(articleIds));
    }

    // 条件分页查询文章列表
    @PostMapping("page")
    public BaseResponse<IPage<ArticleVO>> page(@RequestBody @Validated QueryArticleParam articleParam) {
        return ResultUtils.success(articleService.searchArticlePage(articleParam));
    }

    // 查询文章作者
    @GetMapping("author/{articleId}")
    public BaseResponse<UserVO> author(@PathVariable
                                       @NotNull(message = "文章id不能为空")
                                       Long articleId) {
        return ResultUtils.success(articleService.searchAuthorByArticleId(articleId));
    }

    // 后台管理-分页查询
    @PostMapping("manage/page")
    public BaseResponse<IPage<Article>> managePage(@RequestBody
                                                   @Validated
                                                   QueryManageArticleParam articleParam) {
        return ResultUtils.success(articleService.searchManageArticlePage(articleParam));
    }

    // 修改文章状态
    @PutMapping("manage/status")
    public BaseResponse<String> updateStatus(@RequestBody @Validated ArticleStatusParam statusParam) {
        return ResultUtils.success(articleService.updateStatus(statusParam));
    }

    // 保存文章信息
    @PostMapping
    public BaseResponse<String> saveArticle(@RequestBody @Validated SaveArticleParam saveArticleParam) {
        return ResultUtils.success(articleService.saveArticle(saveArticleParam));
    }
}
